package package01.dao;


import package01.pojo.Chat;
import package01.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者: jack
 * 时间: 2020-07-21 08:56
 * 描述: ChatDaoImp
 */
public class ChatDaoImp implements ChatDao {

    private DBUtil util = new DBUtil();
    private Connection conn;
    private PreparedStatement pst;
    private ResultSet rs;

    /**
     * @param chat
     * @Description 将chat对象的成员变量作为字段存储到数据库中。
     * @Return void
     * @Author zzj
     * @Date 2020/7/21 19:32
     **/
    @Override
    public void save(Chat chat) {
        // 数据库连接
        conn = util.init();
        // 声明sql语句
        String sql = "insert into tb_chat values (?,?,?,?,?)";
        try {
            // 预编译
            pst = conn.prepareStatement(sql);

            pst.setString(1, chat.getId());
            pst.setString(2, chat.getNickname());
            pst.setString(3, chat.getMessage());
            pst.setTimestamp(4, chat.getCreateTime());
            pst.setString(5, chat.getState());
            // 执行sql语句
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库
            util.close();
        }
    }

    /**
     * @param state
     * @Description 查询所有已读的信息，封装成chat对象，作为List集合返回
     * @Return java.util.List<com.iweb.lesson07.pojo.Chat>
     * @Author zzj
     * @Date 2020/7/21 18:57
     **/
    @Override
    public List<Chat> findListByNotRead(String state) {
        // 返回List集合
        List<Chat> list = new ArrayList<>();

        // 连接数据库
        conn = util.init();
        // 自定义sql语言，实际开发过程中禁止使用 * 查询
        String sql = "select * from tb_chat c where c.state = ? order by create_time";
        try {
            // 预编译
            pst = conn.prepareStatement(sql);
            pst.setString(1, state);

            // 执行
            rs = pst.executeQuery();

            while (rs.next()) {
                Chat chat = createChat(rs);
                list.add(chat);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            util.close();
        }
        return list;
    }

    /**
     * @param rs
     * @Description 将结果集封装成chat对象
     * @Return com.iweb.lesson07.pojo.Chat
     * @Author zzj
     * @Date 2020/7/21 19:07
     **/
    private Chat createChat(ResultSet rs) throws Exception {
        Chat chat = new Chat();

        chat.setId(rs.getString("id"));
        chat.setNickname(rs.getString("nickname"));
        chat.setMessage(rs.getString("message"));
        chat.setCreateTime(rs.getTimestamp("create_time"));
        chat.setState(rs.getString("state"));

        return chat;
    }

    @Override
    public void updateState(List<Chat> chats) {

        conn = util.init();
        String sql = "update tb_chat set state = ? where id = ?";
        try {
            pst = conn.prepareStatement(sql);
            for (int i = 0; i < chats.size(); i++) {
                Chat chat = chats.get(i);
                pst.setString(1, "1");
                pst.setString(2, chat.getId());
                pst.addBatch();
            }

            pst.executeBatch();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close();
        }

    }


}
